{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/samlg/.conda/envs/ms-gen/lib/python3.8/site-packages/tqdm/auto.py:22: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n"
     ]
    }
   ],
   "source": [
    "from pathlib import Path\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "from ms_pred.common.plot_utils import *\n",
    "\n",
    "\n",
    "from collections import defaultdict\n",
    "\n",
    "# Import sem calc\n",
    "from scipy.stats import sem\n",
    "\n",
    "\n",
    "set_style()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_names = [\"nist20\", \"canopus_train_public\"]\n",
    "outfolder = \"../results/figs_scarf/coverage/\"\n",
    "outfolder = Path(outfolder)\n",
    "outfolder.mkdir(parents=True, exist_ok=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "names = [\n",
    "    \"SCARF\",\n",
    "    \"SCARF-F\",\n",
    "    \"SCARF-R\",\n",
    "    \"Autoregressive\",\n",
    "    \"CFM-ID\",\n",
    "    \"Random\",\n",
    "    \"Frequency\",\n",
    "]\n",
    "sort_order = {\n",
    "    \"CFM-ID\": 3,\n",
    "    \"Frequency\": 2,\n",
    "    \"Random\": 1,\n",
    "    \"Autoregressive\": 3.05,\n",
    "    \"SCARF\": 4,\n",
    "    \"SCARF-R\": 3.1,\n",
    "    \"SCARF-F\": 3.5,\n",
    "}\n",
    "\n",
    "dataset_to_res = {}\n",
    "for dataset_name in dataset_names:\n",
    "    yaml_files = defaultdict(lambda : [])\n",
    "    for seed in [1,2,3]:    \n",
    "        results_files = [\n",
    "            f\"../results/scarf_{dataset_name}/split_1_rnd{seed}/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/scarf_{dataset_name}_ablate/forward/split_1_rnd{seed}/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/scarf_{dataset_name}_ablate/reverse/split_1_rnd{seed}/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/autoregr_{dataset_name}/split_1_rnd{seed}/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/cfm_id_{dataset_name}/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/rand_baseline_{dataset_name}/split_1/inten_thresh_sweep/summary.tsv\",\n",
    "            f\"../results/freq_baseline_{dataset_name}/split_1/inten_thresh_sweep/summary.tsv\",\n",
    "        ]\n",
    "\n",
    "        for i, j in zip(names, results_files):\n",
    "            yaml_files[i].append(pd.read_csv(j, sep=\"\\t\"))\n",
    "    dataset_to_res[dataset_name] = yaml_files"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "dataset_to_res[\"canopus_train_public\"];"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "combined_df = []\n",
    "# max_preds = [10, 20, 30, 40, 50, 100, 200, 300, 500, 1000]\n",
    "max_preds = [10, 20, 30, 50, 100, 300, 1000]\n",
    "for dataset_name in dataset_names:\n",
    "    cov_dfs = dataset_to_res[dataset_name]\n",
    "    for name, sub_dfs in cov_dfs.items():\n",
    "        for seed_num, sub_df in enumerate(sub_dfs):\n",
    "            for _, row in sub_df.iterrows():\n",
    "                num_nodes = row[\"nm_nodes\"]\n",
    "                if num_nodes not in max_preds:\n",
    "                    continue\n",
    "                coverage = row[\"avg_coverage\"]\n",
    "                digitized_coverage = row[\"avg_digitized_coverage\"]\n",
    "                # sem_coverage = row[\"sem_coverage\"]\n",
    "                avg_num_pred = row[\"avg_num_pred\"]\n",
    "                new_entry = {\n",
    "                    \"Coverage\": coverage,\n",
    "                    # \"SEM Coverage\": sem_coverage,\n",
    "                    \"Method\": name,\n",
    "                    \"Coverage (disc.)\": digitized_coverage,\n",
    "                    \"Num pred.\": avg_num_pred,\n",
    "                    \"Nodes\": num_nodes,\n",
    "                    \"Dataset\": dataset_name,\n",
    "                    \"Seed\": seed_num,\n",
    "                }\n",
    "                combined_df.append(new_entry)\n",
    "\n",
    "new_df = pd.DataFrame(combined_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Groupby dataset, nodes, and method and compute mean and sem\n",
    "grouped_df = new_df.groupby([\"Dataset\", \"Nodes\", \"Method\"]).agg(\n",
    "    {\"Coverage\": [\"mean\", \"sem\"], \"Coverage (disc.)\": [\"mean\", \"sem\"]}\n",
    ")\n",
    "\n",
    "grouped_df = grouped_df.reset_index()\n",
    "grouped_df.columns = [\n",
    "    \"Dataset\",\n",
    "    \"Nodes\",\n",
    "    \"Method\",\n",
    "    \"Coverage\",\n",
    "    \"SEM Coverage\",\n",
    "    \"Coverage (disc.)\",\n",
    "    \"SEM Coverage (disc.)\",\n",
    "]\n",
    "new_df = grouped_df"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Round coverage\n",
    "new_df[\"Coverage\"] = new_df[\"Coverage\"].round(3).fillna(0)\n",
    "new_df[\"SEM Coverage\"] = new_df[\"SEM Coverage\"].round(3).fillna(0)\n",
    "\n",
    "# Create a single column that just has $Coverage \\pm SEM Coverage$ using list comprehension\n",
    "new_df[\"Coverage 95%\"] = [\n",
    "   rf\"${i:.3f} \\pm {j:.3f}$\" for i, j in zip(new_df[\"Coverage\"], new_df[\"SEM Coverage\"])\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Coverage @</th>\n",
       "      <th>10</th>\n",
       "      <th>30</th>\n",
       "      <th>300</th>\n",
       "      <th>1000</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Random</th>\n",
       "      <td>0.004</td>\n",
       "      <td>0.014</td>\n",
       "      <td>0.126</td>\n",
       "      <td>0.336</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Frequency</th>\n",
       "      <td>0.090</td>\n",
       "      <td>0.151</td>\n",
       "      <td>0.466</td>\n",
       "      <td>0.688</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CFM-ID</th>\n",
       "      <td>0.170</td>\n",
       "      <td>0.267</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Autoregressive</th>\n",
       "      <td>0.072</td>\n",
       "      <td>0.082</td>\n",
       "      <td>0.095</td>\n",
       "      <td>0.099</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-R</th>\n",
       "      <td>0.158</td>\n",
       "      <td>0.284</td>\n",
       "      <td>0.681</td>\n",
       "      <td>0.856</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-F</th>\n",
       "      <td>0.155</td>\n",
       "      <td>0.306</td>\n",
       "      <td>0.708</td>\n",
       "      <td>0.859</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF</th>\n",
       "      <td>0.164</td>\n",
       "      <td>0.309</td>\n",
       "      <td>0.724</td>\n",
       "      <td>0.879</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Coverage @         10     30    300   1000\n",
       "Random          0.004  0.014  0.126  0.336\n",
       "Frequency       0.090  0.151  0.466  0.688\n",
       "CFM-ID          0.170  0.267    NaN    NaN\n",
       "Autoregressive  0.072  0.082  0.095  0.099\n",
       "SCARF-R         0.158  0.284  0.681  0.856\n",
       "SCARF-F         0.155  0.306  0.708  0.859\n",
       "SCARF           0.164  0.309  0.724  0.879"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{table}\n",
      "\\centering\n",
      "\\caption{Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the \\gnpsData dataset.}\n",
      "\\label{tab:coverage_canopus_train_public}\n",
      "\\begin{tabular}{lrrrr}\n",
      "\\toprule\n",
      "Coverage @ &     10 &     30 &    300 &   1000 \\\\\n",
      "\\midrule\n",
      "Random         &  0.004 &  0.014 &  0.126 &  0.336 \\\\\n",
      "Frequency      &  0.090 &  0.151 &  0.466 &  0.688 \\\\\n",
      "CFM-ID         &  0.170 &  0.267 &     -- &     -- \\\\\n",
      "Autoregressive &  0.072 &  0.082 &  0.095 &  0.099 \\\\\n",
      "SCARF-R        &  0.158 &  0.284 &  0.681 &  0.856 \\\\\n",
      "SCARF-F        &  0.155 &  0.306 &  0.708 &  0.859 \\\\\n",
      "SCARF          &  0.164 &  0.309 &  0.724 &  0.879 \\\\\n",
      "\\bottomrule\n",
      "\\end{tabular}\n",
      "\\end{table}\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2256379/3885390425.py:20: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.\n",
      "  tex_table = round_df_pivot.to_latex(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Coverage @</th>\n",
       "      <th>10</th>\n",
       "      <th>30</th>\n",
       "      <th>300</th>\n",
       "      <th>1000</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Random</th>\n",
       "      <td>0.009</td>\n",
       "      <td>0.026</td>\n",
       "      <td>0.232</td>\n",
       "      <td>0.532</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Frequency</th>\n",
       "      <td>0.173</td>\n",
       "      <td>0.275</td>\n",
       "      <td>0.659</td>\n",
       "      <td>0.830</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CFM-ID</th>\n",
       "      <td>0.197</td>\n",
       "      <td>0.282</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Autoregressive</th>\n",
       "      <td>0.204</td>\n",
       "      <td>0.262</td>\n",
       "      <td>0.309</td>\n",
       "      <td>0.317</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-R</th>\n",
       "      <td>0.248</td>\n",
       "      <td>0.425</td>\n",
       "      <td>0.839</td>\n",
       "      <td>0.941</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-F</th>\n",
       "      <td>0.249</td>\n",
       "      <td>0.476</td>\n",
       "      <td>0.855</td>\n",
       "      <td>0.943</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF</th>\n",
       "      <td>0.308</td>\n",
       "      <td>0.552</td>\n",
       "      <td>0.907</td>\n",
       "      <td>0.968</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Coverage @         10     30    300   1000\n",
       "Random          0.009  0.026  0.232  0.532\n",
       "Frequency       0.173  0.275  0.659  0.830\n",
       "CFM-ID          0.197  0.282    NaN    NaN\n",
       "Autoregressive  0.204  0.262  0.309  0.317\n",
       "SCARF-R         0.248  0.425  0.839  0.941\n",
       "SCARF-F         0.249  0.476  0.855  0.943\n",
       "SCARF           0.308  0.552  0.907  0.968"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{table}\n",
      "\\centering\n",
      "\\caption{Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the \\nistData dataset.}\n",
      "\\label{tab:coverage_nist20}\n",
      "\\begin{tabular}{lrrrr}\n",
      "\\toprule\n",
      "Coverage @ &     10 &     30 &    300 &   1000 \\\\\n",
      "\\midrule\n",
      "Random         &  0.009 &  0.026 &  0.232 &  0.532 \\\\\n",
      "Frequency      &  0.173 &  0.275 &  0.659 &  0.830 \\\\\n",
      "CFM-ID         &  0.197 &  0.282 &     -- &     -- \\\\\n",
      "Autoregressive &  0.204 &  0.262 &  0.309 &  0.317 \\\\\n",
      "SCARF-R        &  0.248 &  0.425 &  0.839 &  0.941 \\\\\n",
      "SCARF-F        &  0.249 &  0.476 &  0.855 &  0.943 \\\\\n",
      "SCARF          &  0.308 &  0.552 &  0.907 &  0.968 \\\\\n",
      "\\bottomrule\n",
      "\\end{tabular}\n",
      "\\end{table}\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2256379/3885390425.py:20: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.\n",
      "  tex_table = round_df_pivot.to_latex(\n"
     ]
    }
   ],
   "source": [
    "for dataset_name, temp_df in new_df.groupby(\"Dataset\"):\n",
    "    new_df_round = temp_df  # .round(3)\n",
    "\n",
    "    # Filter df round to only have rows where Coverage is in [10, 30, 300, 10000]\n",
    "    new_df_round = new_df_round[new_df_round[\"Nodes\"].isin([10, 30, 300, 1000])]\n",
    "\n",
    "    round_df_pivot = new_df_round.pivot_table(\n",
    "        index=\"Method\", columns=[\"Nodes\"], values=[\"Coverage\"], aggfunc=lambda x: x\n",
    "    )\n",
    "    # display(round_df_pivot)\n",
    "    round_df_pivot.columns = [f\"{int(i[1])}\" for i in round_df_pivot.columns]\n",
    "    round_df_pivot.index.name = None\n",
    "    round_df_pivot.columns.name = \"Coverage @\"\n",
    "    round_df_pivot = round_df_pivot.sort_index(key=lambda x: [sort_order[i] for i in x])\n",
    "    display(round_df_pivot)\n",
    "    data_str = {\"canopus_train_public\": r\"\\gnpsData\", \"nist20\": r\"\\nistData\"}[\n",
    "        dataset_name\n",
    "    ]\n",
    "\n",
    "    tex_table = round_df_pivot.to_latex(\n",
    "        na_rep=\"--\",\n",
    "        label=f\"tab:coverage_{dataset_name}\",\n",
    "        caption=rf\"Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the {data_str} dataset.\",\n",
    "        escape=False,\n",
    "    )\n",
    "    print(tex_table)\n",
    "    with open(outfolder / f\"tab_coverage_{dataset_name}.tex\", \"w\") as f:\n",
    "        f.write(tex_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Coverage @</th>\n",
       "      <th>10</th>\n",
       "      <th>30</th>\n",
       "      <th>300</th>\n",
       "      <th>1000</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Random</th>\n",
       "      <td>$0.004 \\pm 0.000$</td>\n",
       "      <td>$0.014 \\pm 0.000$</td>\n",
       "      <td>$0.126 \\pm 0.000$</td>\n",
       "      <td>$0.336 \\pm 0.000$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Frequency</th>\n",
       "      <td>$0.090 \\pm 0.000$</td>\n",
       "      <td>$0.151 \\pm 0.000$</td>\n",
       "      <td>$0.466 \\pm 0.000$</td>\n",
       "      <td>$0.688 \\pm 0.000$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CFM-ID</th>\n",
       "      <td>$0.170 \\pm 0.000$</td>\n",
       "      <td>$0.267 \\pm 0.000$</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Autoregressive</th>\n",
       "      <td>$0.072 \\pm 0.001$</td>\n",
       "      <td>$0.082 \\pm 0.002$</td>\n",
       "      <td>$0.095 \\pm 0.001$</td>\n",
       "      <td>$0.099 \\pm 0.000$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-R</th>\n",
       "      <td>$0.158 \\pm 0.001$</td>\n",
       "      <td>$0.284 \\pm 0.003$</td>\n",
       "      <td>$0.681 \\pm 0.002$</td>\n",
       "      <td>$0.856 \\pm 0.002$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-F</th>\n",
       "      <td>$0.155 \\pm 0.002$</td>\n",
       "      <td>$0.306 \\pm 0.002$</td>\n",
       "      <td>$0.708 \\pm 0.003$</td>\n",
       "      <td>$0.859 \\pm 0.001$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF</th>\n",
       "      <td>$0.164 \\pm 0.009$</td>\n",
       "      <td>$0.309 \\pm 0.014$</td>\n",
       "      <td>$0.724 \\pm 0.013$</td>\n",
       "      <td>$0.879 \\pm 0.004$</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Coverage @                     10                 30                300  \\\n",
       "Random          $0.004 \\pm 0.000$  $0.014 \\pm 0.000$  $0.126 \\pm 0.000$   \n",
       "Frequency       $0.090 \\pm 0.000$  $0.151 \\pm 0.000$  $0.466 \\pm 0.000$   \n",
       "CFM-ID          $0.170 \\pm 0.000$  $0.267 \\pm 0.000$                NaN   \n",
       "Autoregressive  $0.072 \\pm 0.001$  $0.082 \\pm 0.002$  $0.095 \\pm 0.001$   \n",
       "SCARF-R         $0.158 \\pm 0.001$  $0.284 \\pm 0.003$  $0.681 \\pm 0.002$   \n",
       "SCARF-F         $0.155 \\pm 0.002$  $0.306 \\pm 0.002$  $0.708 \\pm 0.003$   \n",
       "SCARF           $0.164 \\pm 0.009$  $0.309 \\pm 0.014$  $0.724 \\pm 0.013$   \n",
       "\n",
       "Coverage @                   1000  \n",
       "Random          $0.336 \\pm 0.000$  \n",
       "Frequency       $0.688 \\pm 0.000$  \n",
       "CFM-ID                        NaN  \n",
       "Autoregressive  $0.099 \\pm 0.000$  \n",
       "SCARF-R         $0.856 \\pm 0.002$  \n",
       "SCARF-F         $0.859 \\pm 0.001$  \n",
       "SCARF           $0.879 \\pm 0.004$  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{table}\n",
      "\\centering\n",
      "\\caption{Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the \\gnpsData dataset.}\n",
      "\\label{tab:coverage_canopus_train_public}\n",
      "\\begin{tabular}{lllll}\n",
      "\\toprule\n",
      "Coverage @ &                 10 &                 30 &                300 &               1000 \\\\\n",
      "\\midrule\n",
      "Random         &  $0.004 \\pm 0.000$ &  $0.014 \\pm 0.000$ &  $0.126 \\pm 0.000$ &  $0.336 \\pm 0.000$ \\\\\n",
      "Frequency      &  $0.090 \\pm 0.000$ &  $0.151 \\pm 0.000$ &  $0.466 \\pm 0.000$ &  $0.688 \\pm 0.000$ \\\\\n",
      "CFM-ID         &  $0.170 \\pm 0.000$ &  $0.267 \\pm 0.000$ &                 -- &                 -- \\\\\n",
      "Autoregressive &  $0.072 \\pm 0.001$ &  $0.082 \\pm 0.002$ &  $0.095 \\pm 0.001$ &  $0.099 \\pm 0.000$ \\\\\n",
      "SCARF-R        &  $0.158 \\pm 0.001$ &  $0.284 \\pm 0.003$ &  $0.681 \\pm 0.002$ &  $0.856 \\pm 0.002$ \\\\\n",
      "SCARF-F        &  $0.155 \\pm 0.002$ &  $0.306 \\pm 0.002$ &  $0.708 \\pm 0.003$ &  $0.859 \\pm 0.001$ \\\\\n",
      "SCARF          &  $0.164 \\pm 0.009$ &  $0.309 \\pm 0.014$ &  $0.724 \\pm 0.013$ &  $0.879 \\pm 0.004$ \\\\\n",
      "\\bottomrule\n",
      "\\end{tabular}\n",
      "\\end{table}\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2256379/2748585661.py:20: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.\n",
      "  tex_table = round_df_pivot.to_latex(\n"
     ]
    },
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th>Coverage @</th>\n",
       "      <th>10</th>\n",
       "      <th>30</th>\n",
       "      <th>300</th>\n",
       "      <th>1000</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>Random</th>\n",
       "      <td>$0.009 \\pm 0.000$</td>\n",
       "      <td>$0.026 \\pm 0.000$</td>\n",
       "      <td>$0.232 \\pm 0.000$</td>\n",
       "      <td>$0.532 \\pm 0.000$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Frequency</th>\n",
       "      <td>$0.173 \\pm 0.000$</td>\n",
       "      <td>$0.275 \\pm 0.000$</td>\n",
       "      <td>$0.659 \\pm 0.000$</td>\n",
       "      <td>$0.830 \\pm 0.000$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>CFM-ID</th>\n",
       "      <td>$0.197 \\pm 0.000$</td>\n",
       "      <td>$0.282 \\pm 0.000$</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Autoregressive</th>\n",
       "      <td>$0.204 \\pm 0.001$</td>\n",
       "      <td>$0.262 \\pm 0.002$</td>\n",
       "      <td>$0.309 \\pm 0.005$</td>\n",
       "      <td>$0.317 \\pm 0.006$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-R</th>\n",
       "      <td>$0.248 \\pm 0.001$</td>\n",
       "      <td>$0.425 \\pm 0.002$</td>\n",
       "      <td>$0.839 \\pm 0.002$</td>\n",
       "      <td>$0.941 \\pm 0.001$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF-F</th>\n",
       "      <td>$0.249 \\pm 0.001$</td>\n",
       "      <td>$0.476 \\pm 0.002$</td>\n",
       "      <td>$0.855 \\pm 0.000$</td>\n",
       "      <td>$0.943 \\pm 0.001$</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>SCARF</th>\n",
       "      <td>$0.308 \\pm 0.002$</td>\n",
       "      <td>$0.552 \\pm 0.001$</td>\n",
       "      <td>$0.907 \\pm 0.002$</td>\n",
       "      <td>$0.968 \\pm 0.001$</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "Coverage @                     10                 30                300  \\\n",
       "Random          $0.009 \\pm 0.000$  $0.026 \\pm 0.000$  $0.232 \\pm 0.000$   \n",
       "Frequency       $0.173 \\pm 0.000$  $0.275 \\pm 0.000$  $0.659 \\pm 0.000$   \n",
       "CFM-ID          $0.197 \\pm 0.000$  $0.282 \\pm 0.000$                NaN   \n",
       "Autoregressive  $0.204 \\pm 0.001$  $0.262 \\pm 0.002$  $0.309 \\pm 0.005$   \n",
       "SCARF-R         $0.248 \\pm 0.001$  $0.425 \\pm 0.002$  $0.839 \\pm 0.002$   \n",
       "SCARF-F         $0.249 \\pm 0.001$  $0.476 \\pm 0.002$  $0.855 \\pm 0.000$   \n",
       "SCARF           $0.308 \\pm 0.002$  $0.552 \\pm 0.001$  $0.907 \\pm 0.002$   \n",
       "\n",
       "Coverage @                   1000  \n",
       "Random          $0.532 \\pm 0.000$  \n",
       "Frequency       $0.830 \\pm 0.000$  \n",
       "CFM-ID                        NaN  \n",
       "Autoregressive  $0.317 \\pm 0.006$  \n",
       "SCARF-R         $0.941 \\pm 0.001$  \n",
       "SCARF-F         $0.943 \\pm 0.001$  \n",
       "SCARF           $0.968 \\pm 0.001$  "
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\\begin{table}\n",
      "\\centering\n",
      "\\caption{Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the \\nistData dataset.}\n",
      "\\label{tab:coverage_nist20}\n",
      "\\begin{tabular}{lllll}\n",
      "\\toprule\n",
      "Coverage @ &                 10 &                 30 &                300 &               1000 \\\\\n",
      "\\midrule\n",
      "Random         &  $0.009 \\pm 0.000$ &  $0.026 \\pm 0.000$ &  $0.232 \\pm 0.000$ &  $0.532 \\pm 0.000$ \\\\\n",
      "Frequency      &  $0.173 \\pm 0.000$ &  $0.275 \\pm 0.000$ &  $0.659 \\pm 0.000$ &  $0.830 \\pm 0.000$ \\\\\n",
      "CFM-ID         &  $0.197 \\pm 0.000$ &  $0.282 \\pm 0.000$ &                 -- &                 -- \\\\\n",
      "Autoregressive &  $0.204 \\pm 0.001$ &  $0.262 \\pm 0.002$ &  $0.309 \\pm 0.005$ &  $0.317 \\pm 0.006$ \\\\\n",
      "SCARF-R        &  $0.248 \\pm 0.001$ &  $0.425 \\pm 0.002$ &  $0.839 \\pm 0.002$ &  $0.941 \\pm 0.001$ \\\\\n",
      "SCARF-F        &  $0.249 \\pm 0.001$ &  $0.476 \\pm 0.002$ &  $0.855 \\pm 0.000$ &  $0.943 \\pm 0.001$ \\\\\n",
      "SCARF          &  $0.308 \\pm 0.002$ &  $0.552 \\pm 0.001$ &  $0.907 \\pm 0.002$ &  $0.968 \\pm 0.001$ \\\\\n",
      "\\bottomrule\n",
      "\\end{tabular}\n",
      "\\end{table}\n",
      "\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/tmp/ipykernel_2256379/2748585661.py:20: FutureWarning: In future versions `DataFrame.to_latex` is expected to utilise the base implementation of `Styler.to_latex` for formatting and rendering. The arguments signature may therefore change. It is recommended instead to use `DataFrame.style.to_latex` which also contains additional functionality.\n",
      "  tex_table = round_df_pivot.to_latex(\n"
     ]
    }
   ],
   "source": [
    "for dataset_name, temp_df in new_df.groupby(\"Dataset\"):\n",
    "    new_df_round = temp_df  # .round(3)\n",
    "\n",
    "    # Filter df round to only have rows where Coverage is in [10, 30, 300, 10000]\n",
    "    new_df_round = new_df_round[new_df_round[\"Nodes\"].isin([10, 30, 300, 1000])]\n",
    "\n",
    "    round_df_pivot = new_df_round.pivot_table(\n",
    "        index=\"Method\", columns=[\"Nodes\"], values=[\"Coverage 95%\"], aggfunc=lambda x: x\n",
    "    )\n",
    "    # display(round_df_pivot)\n",
    "    round_df_pivot.columns = [f\"{int(i[1])}\" for i in round_df_pivot.columns]\n",
    "    round_df_pivot.index.name = None\n",
    "    round_df_pivot.columns.name = \"Coverage @\"\n",
    "    round_df_pivot = round_df_pivot.sort_index(key=lambda x: [sort_order[i] for i in x])\n",
    "    display(round_df_pivot)\n",
    "    data_str = {\"canopus_train_public\": r\"\\gnpsData\", \"nist20\": r\"\\nistData\"}[\n",
    "        dataset_name\n",
    "    ]\n",
    "\n",
    "    tex_table = round_df_pivot.to_latex(\n",
    "        na_rep=\"--\",\n",
    "        label=f\"tab:coverage_{dataset_name}\",\n",
    "        caption=rf\"Model coverage of true peak formulae as determined by \\MAGMA at various max formula cutoffs for the {data_str} dataset.\",\n",
    "        escape=False,\n",
    "    )\n",
    "    print(tex_table)\n",
    "    with open(outfolder / f\"tab_coverage_{dataset_name}_95.tex\", \"w\") as f:\n",
    "        f.write(tex_table)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ms-gen",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "0104df62c80ee1845876b5ec5b2a8b2e89ea0f28159e1f86b83bf27a253591c7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
